// 引入js-cookie
import cookie from 'js-cookie'
import router from './routers'
import store from '../store'


// 设置白名单
const whiteList = ['/login', '/register', '/email', '/mobile']
const hasOneOfpath = (url, arr) => {
  return arr.some(item => item.includes(url))
}
// 全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = `${to.meta.title}-后台管理系统`
  }else{
    document.title = '后台管理系统'
  }
  // 判断是否有token
  if (cookie.get('token')) {
    // 登录后访问白名单的不可以访问
    if (hasOneOfpath(to.name, whiteList)) {
      next('/select')
    } else {
      next()
    }
    if (cookie.set('role') == 'network') {
      // 当前角色权限
      if (to.name == 'create') {
        next('/data/role')
      }
    } else if (cookie.set('role') == 'consult') {
      // 当前角色权限
      if (to.name == 'label') {
        next('/data/role')
      }
    }
  } else {
    // 没登录访问白名单的就可以访问
    if (hasOneOfpath(to.name, whiteList)) {
      next()
    } else {
      next('/login')
    }
  }
})

// 路由加载完成后
router.afterEach((to, from) => {
  console.log('加载完了')
})
export default router
